﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UMLTouch.Business.MVC;
using UMLTouch.Core.Entities;
using UMLTouch.Core.UI;

namespace UMLTouch.UI.Main
{
    /// <summary>
    /// Interaction logic for DiagramControl.xaml
    /// </summary>
    public partial class DiagramControl : UserControl
    {

        IUMLTouchView _view;
        String _context;


        public IUMLTouchView View
        {
            get { return _view; }
            protected set { _view = value; }
        }

        public String Context
        {
            get { return _context; }
            set { _context = value; }
        }



        public DiagramControl()
        {
            InitializeComponent();

        }

        public DiagramControl(IUMLTouchView view, String context)
            : this()
        {
            _view = view;
            _context = context;

            view.Model.ItemRecognized += new EventHandler<ItemAddedEventArgs>(Model_ItemRecognized);
            view.Model.OnUnknownItemDetected += new EventHandler<UnknownItemAddedEventArgs>(Model_OnUnknownItemDetected);
        }


        void Model_OnUnknownItemDetected(object sender, UnknownItemAddedEventArgs e)
        {
            if (e.Context.Name == _context)
            {
                Dispatcher.Invoke(new Action(() =>
                {

                    ic_strokes.Strokes.Clear();

                }
               ));
            }
        }

        void Model_ItemRecognized(object sender, ItemAddedEventArgs e)
        {
            if (e.Context.Name == _context)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    AddControl(e);

                }
               ));
            }
        }

        private void AddControl(ItemAddedEventArgs e)
        {
            ic_strokes.Strokes.Clear();
            

            try
            {

                var existingControls = ic_strokes.Children.OfType <IAnchorable>().ToList();
                var thisControl = e.Control as IAnchorable;

                var q = (from existingControl in existingControls
                        where thisControl.ConnectItem(existingControl)
                        select existingControl).ToList();



                //var q = from existingAnchor in existingControls
                //        from newAnchor in existingAnchor.Anchors
                //        from anc in thisControl.Anchors
                //        let dist = (newAnchor.Position - anc.Position).Length
                //        group new { newAnchor, anc, dist } by anc into groups
                //        from p in groups
                //        orderby p.dist
                //        select groups;


                
               
            }
            catch (Exception ex)
            {
                Console.WriteLine("oops :" + ex.Message);
                
            }


            //foreach (var uhb in (e.Control as IAnchorable).Anchors)
            //{
            //    DrawPoint(uhb.Position);
            //}

            //e.Control.Visibility = Visibility.Hidden;
            ic_strokes.Children.Add(e.Control);
            
        }
    

        private Point DrawPoint(Point p)
        {
            double w = 10d;
            double h = 10d;

            Ellipse ellipse = new Ellipse();

            ellipse.SetValue(InkCanvas.LeftProperty, p.X - w / 2);

            ellipse.SetValue(InkCanvas.TopProperty, p.Y - h / 2);

            ellipse.Width = w;

            ellipse.Height = h;

            ellipse.Fill = new SolidColorBrush(Colors.Red);

            ic_strokes.Children.Add(ellipse);
            return p;
        }
        

        private void ic_strokes_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            
            Stroke s = new Stroke(e.Stroke);

            if (s.Length > 20)
            {
                _view.Controler.InputStroke(s, _context);
            }
            else
            {
                ic_strokes.Strokes.Remove(e.Stroke);
            }
          
        }


        public void switchEditMode()
        {
            switch (ic_strokes.EditingMode)
            {
               
                case InkCanvasEditingMode.Ink:
                    ic_strokes.EditingMode = InkCanvasEditingMode.Select;
                    break;
                case InkCanvasEditingMode.Select:
                    ic_strokes.EditingMode = InkCanvasEditingMode.Ink;
                    break;
                default:
                    ic_strokes.EditingMode = InkCanvasEditingMode.Ink;
                    break;
            }
        }

        private void ic_strokes_SelectionMoved(object sender, EventArgs e)
        {
            
        }

        private void ic_strokes_SelectionMoving(object sender, InkCanvasSelectionEditingEventArgs e)
        {
            Vector v = e.NewRectangle.TopLeft - e.OldRectangle.TopLeft ;
            foreach (var item in ic_strokes.GetSelectedElements().OfType<IAnchorable>())
            {
                item.Move(v);
            }
        }

      


    }
}
